package validation.jexp;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.beans.Model;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * JGoodies Binding: Bean Adapter Example
 */
public class BeanAdapterExample extends JPanel {
    private PersonBean personBean;

    public BeanAdapterExample() {
        DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:g"));
        defaultFormBuilder.setDefaultDialogBorder();
        this.personBean = new PersonBean("Scott", "Delap");
        BeanAdapter beanAdapter = new BeanAdapter(this.personBean, true);
        ValueModel firstNameAdapter = beanAdapter.getValueModel("firstName");
        ValueModel lastNameAdapter = beanAdapter.getValueModel("lastName");
        JTextField firstNameField = BasicComponentFactory.createTextField(firstNameAdapter);
        JTextField lastNameField = BasicComponentFactory.createTextField(lastNameAdapter);
        defaultFormBuilder.append("First Name:", firstNameField);
        defaultFormBuilder.append("Last Name:", lastNameField);
        defaultFormBuilder.append(new JButton(new ConvertValueToUpperCaseAction()), 3);
        defaultFormBuilder.append(new JButton(new ConvertValueToLowerCaseAction()), 3);
        defaultFormBuilder.append(new JButton(new ShowValueHolderValueAction()), 3);
        add(defaultFormBuilder.getPanel());
    }

    private class ConvertValueToUpperCaseAction extends AbstractAction {
        private ConvertValueToUpperCaseAction() {
            super("Convert Value To UpperCase");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            personBean.setFirstName(personBean.getFirstName().toUpperCase());
            personBean.setLastName(personBean.getLastName().toUpperCase());
        }
    }

    private class ConvertValueToLowerCaseAction extends AbstractAction {
        private ConvertValueToLowerCaseAction() {
            super("Convert Value To LowerCase");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            personBean.setFirstName(personBean.getFirstName().toLowerCase());
            personBean.setLastName(personBean.getLastName().toLowerCase());
        }
    }

    private class ShowValueHolderValueAction extends AbstractAction {
        private ShowValueHolderValueAction() {
            super("Show Value");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            StringBuffer message = new StringBuffer();
            message.append("<html>");
            message.append("<b>First Name:</b>");
            message.append(personBean.getFirstName());
            message.append("<br><b>Last Name:</b>");
            message.append(personBean.getLastName());
            message.append("</html>");
            JOptionPane.showMessageDialog(null, message.toString());
        }
    }



    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JGoodiesValidationUtils.showFrame("Bean Adapter Example", new BeanAdapterExample());
        });
    }
}
